package com.ccys.doctor.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.entity.TblBooking;
import com.ccys.db.entity.TblDoctor;
import com.ccys.db.entity.TblDoctorAppointmentSetting;
import com.ccys.db.enums.FamilyRelationEnum;
import com.ccys.db.mapper.TblBookingMapper;
import com.ccys.db.mapper.TblDoctorAppointmentSettingMapper;
import com.ccys.db.mapper.TblDoctorMapper;
import com.ccys.db.mapper.TblMemberMapper;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BookingBasicService;
import com.ccys.db.vo.doctor.login.ProfileVo;
import com.ccys.doctor.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author：XLY
 * @Date：2024/2/7 0007
 */
@Service
public class DoctorServiceImpl extends BaseServiceImpl<TblDoctor> implements DoctorService {

    @Resource
    private TblDoctorMapper doctorMapper;

    @Resource
    private TblDoctorAppointmentSettingMapper appointmentSettingMapper;

    @Resource
    private TblBookingMapper bookingMapper;

    @Resource
    private TblMemberMapper memberMapper;

    @Autowired
    private BookingBasicService bookingBasicService;

    @Override
    public ResponseResultApp getProfile() {
        ProfileVo vo = doctorMapper.selectDoctorProfile(UserUtil.getCurLoginUserId());
        //组装awards
        if(StringUtils.isNotBlank(vo.getAwards())){
            String[] split = vo.getAwards().split(",");
            JSONArray arr = new JSONArray();
            for (String s : split) {
                JSONObject json = new JSONObject();
                json.put("name",s);
                arr.add(json);
            }
            vo.setAwards(arr.toJSONString());
        }

        //组装education
        if(StringUtils.isNotBlank(vo.getEducations())){
            String[] split = vo.getEducations().split(",");
            JSONArray arr = new JSONArray();
            for (String s : split) {
                JSONObject json = new JSONObject();
                json.put("name",s);
                arr.add(json);
            }
            vo.setEducations(arr.toJSONString());
        }

        //组装certifications
        if(StringUtils.isNotBlank(vo.getCertifications())){
            String[] split = vo.getCertifications().split(",");
            JSONArray arr = new JSONArray();
            for (String s : split) {
                JSONObject json = new JSONObject();
                json.put("name",s);
                arr.add(json);
            }
            vo.setCertifications(arr.toJSONString());
        }

        //组装languages
        if(StringUtils.isNotBlank(vo.getLanguages())){
            String[] split = vo.getLanguages().split(",");
            JSONArray arr = new JSONArray();
            for (String s : split) {
                JSONObject json = new JSONObject();
                json.put("language",s);
                arr.add(json);
            }
            vo.setLanguages(arr.toJSONString());
        }

        //is_allow_booking
        if(2==vo.getPanelType()){
            vo.setAllowBooking(true);
            if(vo.isOnlineStatus()&&vo.getStatus()>0&&vo.getJobStatus()>0&&vo.getJobSubStatus()>0
                    &&("106".equals(vo.getCountryOfResidence())||"203".equals(vo.getCountryOfResidence()))){
                vo.setAllowBooking(false);
            }
        }else{
            vo.setAllowBooking(false);
            TblDoctorAppointmentSetting condition = new TblDoctorAppointmentSetting();
            condition.setDoctorId(UserUtil.getCurLoginUserId());
            condition.setDelFlag(0);
            condition.setShiftType(1);
            condition.setForOnlineAppointment(1);
            condition.setForWalkinAppointment(1);
            if(appointmentSettingMapper.selectCount(condition)>0){
                vo.setAllowBooking(true);
            }
        }
        //active_booking 正在问诊的booking
        vo.setActiveBooking(getActiveBooking());

        return ResponseResultApp.success(vo);
    }

    @Override
    public ProfileVo.ActiveBooking getActiveBooking() {
        TblBooking booking = new TblBooking();
        booking.setDoctorId(UserUtil.getCurLoginUserId());
        ProfileVo.ActiveBooking activeBooking = bookingMapper.selectActiveBooking(booking);
        if (ObjectUtil.isNull(activeBooking)){
            return null;
        }
        //获取当前问诊状态的字符串
        activeBooking.setStatus(bookingBasicService.getStatusByBooking(activeBooking.getId()));
        //查询member
        ProfileVo.ActiveBookingMember activeBookingMember = memberMapper.selectActiveBookingMember(activeBooking.getMemId());
        activeBooking.setMember(activeBookingMember);
        //查询是否儿科
        if(StringUtils.isNotBlank(activeBooking.getPatientMemId())&&!activeBooking.getMemId().equals(activeBooking.getPatientMemId())){
            ProfileVo.ActiveBookingChild activeBookingChild = memberMapper.selectActiveBookingChild(activeBooking.getPatientMemId());
            activeBookingChild.setAge(DateUtil.ageOfNow(activeBookingChild.getDateOfBirth()));
            activeBookingChild.setRelationShip(FamilyRelationEnum.getRelation(Integer.valueOf(activeBookingChild.getRelationShip())));
            activeBookingMember.setChild(activeBookingChild);
        }
        activeBooking.setMember(activeBookingMember);
        //查询病人过往两次问诊信息
        List<ProfileVo.ActiveBookingPassConsultation> consultations = bookingMapper.selectPassConsultation(activeBooking.getPatientMemId());
        for (ProfileVo.ActiveBookingPassConsultation consultation : consultations) {
            consultation.setCreatedOn(DateUtil.format(new Date(consultation.getCreatedOnUtc()),"yyyy-MM-dd HH:mm:ss"));
            String symptoms = consultation.getSymptoms();
            if(StringUtils.isNotBlank(symptoms)){
                JSONArray arr = new JSONArray();
                String[] sArr = symptoms.split(",");
                for (String  s: sArr) {
                    JSONObject json = new JSONObject();
                    json.put("name",s);
                    arr.add(json);
                }
                consultation.setSymptoms(arr.toJSONString());
            }
        }
        return activeBooking;
    }


}
